package com.cgydu.controller;

import com.cgydu.entity.*;
import com.cgydu.service.AddressService;
import com.cgydu.service.CartService;
import com.cgydu.service.OrdersService;
import com.cgydu.service.impl.AddressServiceImpl;
import com.cgydu.service.impl.CartServiceImpl;
import com.cgydu.service.impl.OrdersServiceImpl;
import com.cgydu.utils.Constants;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * projectName: day25_store
 * author: CGY
 * time: 2020/9/19 14:55
 * description:用户订单
 */
@WebServlet("/order")
public class OrdersController extends BaseController{
    /**
     * 订单预览功能和添加订单收货地址功能
     * @param request
     * @param response
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {
        //1.获取请求蚕食uid
        String uid = request.getParameter("uid");

        //2.调用业务逻辑（地址，购物车）
        AddressService addressService = new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));

        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));
        //3.放入共享域即可

        request.setAttribute("addressList",addressList);
        request.setAttribute("cartList",cartList);

        return Constants.FORWARD_ACTION + "/order.jsp";
    }

    /**
     * 提交订单成功
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String create(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取请求参数
        String aid = request.getParameter("aid");
        String uid = request.getParameter("uid");
        String sum = request.getParameter("sum");

        //2.调用业务逻辑生成订单

        OrdersService ordersService = new OrdersServiceImpl();
        ordersService.createOrder(aid,uid,sum);

        //3.转发到订单展示的方法

        return Constants.FORWARD_ACTION +"order?method=show";

    }

    /**
     * 展示我的订单生成
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String show(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");

        if (user == null) {
            session.setAttribute("msg", "登录后可以查看订单！");
            return Constants.REDIRECT_ACTION + "/login.jsp";
        }

        OrdersService ordersService = new OrdersServiceImpl();
        List<Orders> ordersList = ordersService.findOrdersByUid(user.getUid());
        request.setAttribute("ordersList", ordersList);
        return Constants.FORWARD_ACTION  + "/orderList.jsp";

    }

    /**
     * 订单详情的查询
     * @param request
     * @param response
     * @return
     */
    public String detail(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        String oid = request.getParameter("oid");

        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.findOrderByOid(oid);
        request.setAttribute("order", order);
        return Constants.FORWARD_ACTION  + "/orderDetail.jsp";
    }

    /**
     * 微信支付功能的实现
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String success(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //1.获取请求参数
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");

        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);

        String result_code = weiXin.getResult().getResult_code();

        if (result_code != null && result_code.equals("SUCCESS"))
        {
            //支付成功
            //修改状态
            //订单列表页面
            OrdersService ordersService = new OrdersServiceImpl();
            ordersService.updateStateByOid(oid);

            return Constants.FORWARD_ACTION + "/order?method=show";
        }else{
            //支付失败
            HttpSession session = request.getSession();
            session.setAttribute("msg", "订单："+oid+" 支付失败！");
            //message.jsp页面
            return Constants.REDIRECT_ACTION + "/message.jsp";
        }
    }

}
